package com.lzb.yabbs.net;

import android.util.Log;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.MultipartBuilder;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;

import org.apache.http.message.BasicNameValuePair;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by liuzaibing on 2015/8/30.
 */
public class OkHttpClientRequest {
    private static final MediaType MEDIA_TYPE_PIC = MediaType.parse("image/*");

    /**
     * 处理post请求
     *
     * @param url
     * @param params
     */
    public static void requestPost(String url, Map<String, String> params, Callback callback) {
        FormEncodingBuilder builder = new FormEncodingBuilder();
        Iterator it = params.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            String value = params.get(key);
            builder.add(key, value);
        }
        RequestBody body = builder.build();
        Request request = new Request.Builder().url(url).post(body).build();
        OkHttpBase.enqueue(request, callback);
    }

    /**
     * 上传头像
     *
     * @param url
     * @param params
     * @param bytes
     * @param callback
     */
    public static void requestAvatarPost(String url, Map<String, String> params, byte[] bytes, Callback callback) {
        MultipartBuilder builder = new MultipartBuilder();
        builder.type(MultipartBuilder.FORM);
        Iterator it = params.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            String value = params.get(key);
            builder.addFormDataPart(key, value);
        }
        builder.addFormDataPart("avatarImg", "avatar.png", RequestBody.create(MEDIA_TYPE_PIC, bytes));
        RequestBody body = builder.build();
        Request request = new Request.Builder().url(url).post(body).build();
        OkHttpBase.enqueue(request, callback);
    }


    /**
     * 处理put请求
     *
     * @param url
     * @param params
     */
    public static void requestPut(String url, Map<String, String> params, Callback callback) {
        FormEncodingBuilder builder = new FormEncodingBuilder();
        Iterator it = params.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            String value = params.get(key);
            builder.add(key, value);
        }
        RequestBody body = builder.build();
        Request request = new Request.Builder().url(url).put(body).build();
        OkHttpBase.enqueue(request, callback);
    }

    /**
     * 处理get请求
     *
     * @param url
     * @param params
     */
    public static void requestGet(String url, List<BasicNameValuePair> params, Callback callback) {
        if(params!=null)
            url = OkHttpBase.attachHttpGetParams(url, params);
        Log.d("url===", url);
        Request request = new Request.Builder().url(url).get().build();
        OkHttpBase.enqueue(request, callback);
    }

    /**
     * 处理delete请求
     *
     * @param url
     * @param params
     */
    public static void requestDelete(String url, List<BasicNameValuePair> params, Callback callback) {
        url = OkHttpBase.attachHttpGetParams(url, params);
        Log.d("delete url ===", url);
        Request request = new Request.Builder().url(url).delete().build();
        OkHttpBase.enqueue(request, callback);
    }
}
